package com.spring_mysql_redis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spring_mysql_redis.entity.*;
import com.spring_mysql_redis.mapper.CommentMapper;
import com.spring_mysql_redis.mapper.DynamicMapper;
import com.spring_mysql_redis.model.ResultFormat;
import com.spring_mysql_redis.service.CommentService;
import com.spring_mysql_redis.utils.RedisUtil;
import com.spring_mysql_redis.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    private Logger logger = LoggerFactory.getLogger(CommentService.class);

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private DynamicMapper dynamicMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List selectCommentList(Integer dynamicId) {
        if(dynamicId != null){
            List<Comment> comments = commentMapper.selectList(new QueryWrapper<Comment>().eq("dynamic_id", dynamicId));
            if (comments != null && comments.size() != 0){
                redisUtil.lSet("comment_list", comments);
            }
            System.out.println(comments+"---------------------------");
            return comments;
        }
        return null;
    }

    @Override
    public Page<DynamicComm> selectDynamicCommList(Page<DynamicComm> dynamicComm, Integer dynamicId) {
        return commentMapper.getCommentList(dynamicComm,dynamicId);
    }

    //用户写评论方法
    @Override
    public ResultFormat saveComment(Comment comment) {

        logger.info("-----------------进入service实现层 添加动态-----------------");

        if (comment.getUserId() != null) {
            if (comment.getContent() != null && !comment.getContent().equals("")) {

                Integer insert = commentMapper.insert(comment);
                updateDynamicComment(comment.getDynamicId());
                if (insert > 0) {
                    redisUtil.lSet("comment_id", comment.getId());
                    redisUtil.set("sp_comment:" + comment.getContent(), comment);
                }
                return ResultUtil.success();

            }else {
                return ResultUtil.error(101, "未填写内容");
            }
        }else {
            return ResultUtil.error(100, "未登录");
        }


    }

    //删除评论
    @Override
    public ResultFormat deleteComment(Comment comment) {
        //因为评论在动态里
        if (comment.getUserId() != null && comment.getId() != null){
            Comment sel = commentMapper.selectOne(new QueryWrapper<Comment>()
                    .eq("user_id", comment.getUserId()).eq("id",comment.getId()));
            if(sel != null){
                Integer del = commentMapper.deleteById(comment.getId());
                //更新动态列数
                updateDynamicComment(comment.getDynamicId());
                return ResultUtil.success();
            }
            return ResultUtil.error(100, "无该数据");
        }
        return ResultUtil.error(100, "错误");
    }

    //更新动态评论数
    @Override
    public ResultFormat updateDynamicComment(Integer dynamicId) {
        //查询动态是否存在
        Dynamic dynamic = dynamicMapper.selectById(dynamicId);
        if(dynamic != null){
            Integer comments = commentMapper.selectCount(new QueryWrapper<Comment>().eq("dynamic_id", dynamicId));
            dynamic.setComments(comments);
            Integer up = dynamicMapper.updateById(dynamic);
            return ResultUtil.success();
        }
        return ResultUtil.error(100, "错误");
    }

    @Override
    public void delCommentList(Integer dynamicId) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<Comment>().eq(Comment::getDynamicId, dynamicId);
        if (wrapper != null){
            commentMapper.delete(wrapper);
        }
    }
}
